package com.ruoyi.back.service.v3.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.date
import com.ruoyi.app.common.format
import com.ruoyi.back.domain.v3.ExperienceRecord
import com.ruoyi.back.domain.v3.Recommend
import com.ruoyi.back.domain.v3.dto.UserExperienceRecordSearchDto
import com.ruoyi.back.domain.v3.vo.RecommendRecordListVo
import com.ruoyi.back.mapper.ExperienceRecordMapper
import com.ruoyi.back.service.v3.ExperienceRecordService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class ExperienceRecordServiceImpl : ExperienceRecordService, ServiceImpl<ExperienceRecordMapper, ExperienceRecord>() {

    override fun saveExperienceRecordInfo(experienceRecord: ExperienceRecord): Data<Boolean> =
        experienceRecord.id?.run {
            Ok("更新成功", updateById(experienceRecord))
        } ?: Ok("保存成功", save(experienceRecord))

    override fun getExperienceRecordList(userExperienceRecordSearchDto: UserExperienceRecordSearchDto): Page<ExperienceRecord> =
        TableSupport.buildPageRequest().let { pageDomain ->

            page(
                Page(
                    pageDomain.pageNum.toLong(),
                    pageDomain.pageSize.toLong()
                ), KtQueryWrapper(ExperienceRecord::class.java)
                    .eq(
                        StrUtil.isNotBlank(userExperienceRecordSearchDto.name),
                        ExperienceRecord::descText,
                        userExperienceRecordSearchDto.name
                    )
                    .eq(userExperienceRecordSearchDto.type != null, ExperienceRecord::type, userExperienceRecordSearchDto.type)
                    .orderByDesc(ExperienceRecord::createTime)
            )
        }

    override fun removeExperienceRecord(ids: String): Data<Boolean> =
        Ok("批量移除成功", removeByIds(ids.split(",")))
}